<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Add_contact extends Page
{
       public $lblfcode = null;
       public $edfcode = null;
       public $Label3 = null;
       public $lblarea = null;
       public $edArea = null;
       public $Label2 = null;
       public $JTJob2 = null;
       public $JTJob1 = null;
       public $JTDivJob = null;
       public $btnDelJob = null;
       public $btnAddJob = null;
       public $listJob = null;
       public $edJob = null;
       public $JTGroupBox1 = null;
       public $btnShowJob = null;
       public $cmbJob = null;
       public $Label20 = null;
       public $Label1 = null;
       public $lblContactMname = null;
       public $JTDept3 = null;
       public $JTDept1 = null;
       public $JTDept2 = null;
       public $JTDivDept = null;
       public $JTGroupBox3 = null;
       public $btnAddDept = null;
       public $btnDelDept = null;
       public $edDept = null;
       public $listDept = null;
       public $compid = null;
       public $btnShowDept = null;
       public $cmbDept = null;
       public $lblDept = null;
       public $lblContactFname = null;
       public $hfError = null;
       public $JSValidate = null;
       public $lblEmail = null;
       public $lblPhone = null;
       public $lblContactDesignation = null;
       public $lblContactLname = null;
       public $hiddenRandom = null;
       public $hiddenComp = null;
       public $Image2 = null;
       public $btnContactCancel = null;
       public $btnSubmit = null;
       public $JTBdate = null;
       public $JTSiteTheme1 = null;
       public $edEmail = null;
       public $edFax = null;
       public $cmbMunicipality = null;
       public $cmbProvince = null;
       public $cmbRegion = null;
       public $edBldg = null;
       public $edAddress = null;
       public $edMobile = null;
       public $edPhone = null;
       public $edContactLname = null;
       public $edContactMname = null;
       public $edContactFname = null;
       public $Label8 = null;
       public $Label30 = null;
       public $Label29 = null;
       public $Label28 = null;
       public $Label27 = null;
       public $Label26 = null;
       public $Label25 = null;
       public $Label24 = null;
       public $Label23 = null;
       public $Label22 = null;
       public $Label21 = null;
       public $Label19 = null;
       public $Label12 = null;
       public $Label10 = null;
       function edfcodeJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       var edArea = document.getElementById('edfcode').value;
       //validateRequire('edArea','lblarea');

       if(edArea.length>0)
       {
       regex_pattern = "^[0-9]{2,4}$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edfcode').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblfcode').innerHTML = "&darr;&nbsp;Incorrect format - e.g. 02&nbsp;&darr;";
       }
       else
       {
        document.getElementById('lblfcode').innerHTML = "&nbsp;";
       }
       }
       <?php

       }

       function edAreaJSBlur($sender, $params)
       {

       ?>
       var edArea = document.getElementById('edArea').value;
       validateRequire('edArea','lblarea');

       if(edArea.length>0)
       {
       regex_pattern = "^[0-9]{2,4}$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edArea').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblarea').innerHTML = "&darr;&nbsp;Incorrect format - e.g. 02&nbsp;&darr;";
       }
       else
       {
        document.getElementById('lblarea').innerHTML = "&nbsp;";
       }
       }
       <?php

       }

       function btnDelJobJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var JobIndex = document.getElementById('listJob').selectedIndex;
       var JobValue = document.getElementById('listJob')[JobIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxJob3(JobValue,comp);
       <?php

       }

       function btnAddJobJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
        var addjob = document.getElementById('edJob').value;
       var comp = document.getElementById('compid').value;
       AjaxJob2(addjob,comp);
       document.getElementById('edJob').value = "";
       <?php

       }

       function btnShowJobJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivJob').Show();
       document.getElementById('edJob').value = "";
       <?php

       }

       function edContactMnameJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edContactMname','lblContactMname');
       <?php

       }

       function btnDelDeptJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var DeptIndex = document.getElementById('listDept').selectedIndex;
       var DeptValue = document.getElementById('listDept')[DeptIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxDept3(DeptValue,comp);
       <?php

       }

       function btnAddDeptJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var adddept = document.getElementById('edDept').value;
       var comp = document.getElementById('compid').value;
       AjaxDept2(adddept,comp);
       document.getElementById('edDept').value = "";
       <?php

       }

       function btnShowDeptJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivDept').Show();
       document.getElementById('edDept').value = "";
       <?php

       }

       function Add_contactJSLoad($sender, $params)
       {
       $this->clr_reg();
       ?>
       //Add your javascript code here

       <?php

       }

       function edEmailJSBlur($sender, $params)
       {
       ?>
       validateRequire('edEmail','lblEmail');

       var edEmail = document.getElementById('edEmail').value;

       if(edEmail.length>0)
       {
       regex_pattern = "[A-Za-z0-9_\.]+[@][A-Za-z-]+[.][A-Za-z]{2,4}([\.A-Za-z]{2,3})?$";
       var regm = new RegExp(regex_pattern);
       var match = document.getElementById('edEmail').value.match(regm);
       if(match==null)
       {
        document.getElementById('lblEmail').innerHTML = "&darr;&nbsp;Incorrect format - e.g.(user@mail.com)&nbsp;&darr;";
       }
       }
       <?php

       }

       function edPhoneJSBlur($sender, $params)
       {

       ?>
       validateRequire('edPhone','lblPhone');

       var edPhone = document.getElementById('edPhone').value;

       if(edPhone.length>0)
       {
       regex_pattern = "^[0-9]{3}[-]?[0-9]{4}$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edPhone').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblPhone').innerHTML = "&darr;&nbsp;Incorrect format - e.g.899-1234&nbsp;&darr;";
       }
       else
       {
        document.getElementById('lblPhone').innerHTML = "";
       }
       }
       <?php

       }

       function edContactDesignationJSBlur($sender, $params)
       {

       ?>
       validateRequire('edContactDesignation','lblContactDesignation');
       <?php

       }

       function edContactLnameJSBlur($sender, $params)
       {

       ?>
       validateRequire('edContactLname','lblContactLname');
       <?php

       }

       function edContactFnameJSBlur($sender, $params)
       {

       ?>
       validateRequire('edContactFname','lblContactFname');
       <?php

       }

       function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->$lbl->Caption = '&darr; This field is required &darr;';
             $this->hfError->setValue('1');
          }
       }
       function cmbProvinceJSChange($sender, $params)
       {
       echo $this->cmbProvince->ajaxCall("VendorProvinceChange");
       ?>
       //Add your javascript code here
       return false;
       <?php

       }

       function cmbRegionJSChange($sender, $params)
       {
       echo $this->cmbRegion->ajaxCall("VendorRegionChange");
       ?>
       //Add your javascript code here
       return false;
       <?php

       }

       function clr_reg()
       {

       //$this->lblContactFname->Caption = '';
       //$this->lblContactLname->Caption = '';
       //$this->lblPhone->Caption = '';
       //$this->lblContactDesignation->Caption = '';
       //$this->lblEmail->Caption = '';

       $this->edContactFname->Text = '';
       $this->edContactMname->Text = '';
       $this->edContactLname->Text = '';
       //$this->edContactDesignation->Text = '';
       $this->edPhone->Text = '';
       $this->edMobile->Text = '';
       $this->edAddress->Text = '';
       $this->edBldg->Text = '';
       $this->edFax->Text = '';
       $this->edEmail->Text = '';
       $this->JTBdate->Text = date('Y-m-d');

       $this->cmbDept->Clear();
       $this->cmbDept->AddItem('--Select--', null, null);
       $this->cmbDept->setItemIndex('--Select--');
        $result_department = mysql_query("select * from lookup_personnel_tbl where company_id = '".$_SESSION['company_id']."' AND category='departments'");

        while($row_department = mysql_fetch_array($result_department))
        {
            $this->cmbDept->AddItem($row_department['value'], null, $row_department['id']);
        }

       $this->cmbRegion->Clear();
       $this->cmbRegion->AddItem('--Select--', null, null);
       $this->cmbRegion->setItemIndex('--Select--');
       $result_vend_region = mysql_query("select * from lookup_region");

        while($row_vend_region = mysql_fetch_array($result_vend_region))
        {
            $this->cmbRegion->AddItem($row_vend_region['region'], null, $row_vend_region['id']);
        }

        $this->cmbProvince->Clear();
        $this->cmbProvince->AddItem('--Select Region First--',null,null);
        $this->cmbMunicipality->Clear();
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }



       function Add_contactCreate($sender, $params)
       {
       $this->compid->Value = $_SESSION['company_id'];
       $comp_id = $_GET['comp_id'];
       $this->hiddenComp->setValue($comp_id);
       $this->hfError->setValue('0');
       $this->lblContactMname->Caption = '';
       $this->lblContactFname->Caption = '';
       $this->lblContactLname->Caption = '';
       $this->lblPhone->Caption = '';
       $this->lblContactDesignation->Caption = '';
       $this->lblEmail->Caption = '';
       $this->lblarea->Caption = '';
       $this->lblfcode->Caption = '';

       $this->edContactFname->Text = '';
       $this->edContactMname->Text = '';
       $this->edContactLname->Text = '';
       //$this->edContactDesignation->Text = '';
       $this->edArea->Text ='';
       $this->edPhone->Text = '';
       $this->edMobile->Text = '';
       $this->edAddress->Text = '';
       $this->edBldg->Text = '';
       $this->edFax->Text = '';
       $this->edEmail->Text = '';
       $this->JTBdate->Text ='';
       $this->edfcode->Text = '';

       $this->cmbJob->Clear();
       $this->cmbJob->AddItem('--Select--', null, null);
       $this->cmbJob->setItemIndex('--Select--');
        $result_job = mysql_query("select * from lookup_personnel_tbl where company_id='".$_SESSION['company_id']."' AND category='job titles' order by value asc");

        while($row_job = mysql_fetch_array($result_job))
        {
            $this->cmbJob->AddItem($row_job['value'], null, $row_job['value']);
        }

        //list job
        $this->listJob->Clear();
        $result_jt = mysql_query("select * from lookup_personnel_tbl where company_id='".$_SESSION['company_id']."' AND category='job titles' order by value asc");

        while($row_jt = mysql_fetch_array($result_jt))
        {
            $this->listJob->AddItem($row_jt['value'], null, $row_jt['value']);
        }

       $this->cmbDept->Clear();
       $this->cmbDept->AddItem('--Select--', null, null);
       $result_department = mysql_query("select * from lookup_personnel_tbl where company_id = '".$_SESSION['company_id']."' AND category='departments'");

       while($row_department = mysql_fetch_array($result_department))
        {
            $this->cmbDept->AddItem($row_department['value'], null, $row_department['id']);
        }

       //list department
       $this->listDept->Clear();
       $result_dept = mysql_query("select * from lookup_personnel_tbl where company_id = '".$_SESSION['company_id']."' AND category='departments'");

       while($row_dept = mysql_fetch_array($result_dept))
        {
            $this->listDept->AddItem($row_dept['value'], null, $row_dept['id']);
        }
       }


       function VendorRegionChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       if($region_id==0)
       {
        $this->cmbProvince->Clear();
        $this->cmbMunicipality->Clear();
        $this->cmbProvince->AddItem('--Select Region First--',null,null);
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }


       function VendorProvinceChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       $province_id = $this->cmbProvince->getItemIndex();
       $this->cmbMunicipality->Clear();
       $this->cmbMunicipality->AddItem('--Select--', null, null);
       $result_municipality = mysql_query("select * from lookup_municipality where province_id='".$province_id."'");

       while($row_municipality = mysql_fetch_array($result_municipality))
       {
            $this->cmbMunicipality->AddItem($row_municipality['municipality'], null, $row_municipality['id']);
       }

       if($province_id==0)
       {
        $this->cmbMunicipality->Clear();
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }




       function btnSubmitClick($sender, $params)
       {
       //$this->validateRequired($this->edContactDesignation->Text, 'lblContactDesignation');
       $this->validateRequired($this->edContactLname->Text, 'lblContactLname');
       //$this->validateRequired($this->edContactMname->Text, 'lblContactMname');
       $this->validateRequired($this->edContactFname->Text, 'lblContactFname');
       //$this->validateRequired($this->edPhone->Text, 'lblPhone');
       //$this->validateRequired($this->edEmail->Text, 'lblEmail');
       $phone = $this->edPhone->Text;
       $this->validateRequired($this->edPhone->Text, 'lblPhone');

       if($phone!='')
       {
       if(preg_match("/^[0-9]{3}[-]?[0-9]{4}$/", $phone))
       {
       $this->lblPhone->Caption = '';
       }
       else
       {
       $this->lblPhone->Caption = '&darr;&nbsp;Incorrect format - e.g.899-1234&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $acode = $this->edArea->Text;
       $this->validateRequired($this->edArea->Text, 'lblarea');
       if($acode!='')
       {
       if(preg_match("/^[0-9]{2,4}$/", $acode))
       {
       $this->lblarea->Caption = '';
       }
       else
       {
       $this->lblarea->Caption = '&darr;&nbsp;Incorrect format - e.g.02&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $fcode = $this->edfcode->Text;
       if($fcode!='')
       {
       if(preg_match("/^[0-9]{2,4}$/", $fcode))
       {
       $this->lblfcode->Caption = '';
       }
       else
       {
       $this->lblfcode->Caption = '&darr;&nbsp;Incorrect format - e.g.02&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $email = $this->edEmail->Text;
       /*if($email=='')
       $this->validateRequired($this->edEmail->Text, 'lblEmail');*/

       if($email!='')
       {
       if(preg_match("/[A-Za-z0-9_\.]+[@][A-Za-z-]+[.][A-Za-z]{2,4}([\.A-Za-z]{2,3})?$/", $email))
       {
       $this->lblEmail->Caption = '';
       }
       else
       {
       $this->lblEmail->Caption = '&darr;&nbsp;Incorrect format - e.g.(user@mail.com)&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }


       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {

       if($this->hiddenComp->getValue()==0)
       {
       mysql_query("INSERT INTO company_contact(company_id,
       fname,
       mname,
       lname,
       designation,
       department,
       bdate,
       acode,
       phone,
       mobile,
       address,
       bldg,
       region,
       province,
       municipality,
       fcode,
       fax,
       email)

       VALUES
       ('".$_SESSION['rand']."',
       '".$this->edContactFname->Text."',
       '".$this->edContactMname->Text."',
       '".$this->edContactLname->Text."',
       '".$this->cmbJob->getItemIndex()."',
       '".$this->cmbDept->getItemIndex()."',
       '".$this->JTBdate->Text."',
       '".$acode."',
       '".$this->edPhone->Text."',
       '".$this->edMobile->Text."',
       '".$this->edAddress->Text."',
       '".$this->edBldg->Text."',
       '".$this->cmbRegion->getItemIndex()."',
       '".$this->cmbProvince->getItemIndex()."',
       '".$this->cmbMunicipality->getItemIndex()."',
       '".$this->edfcode->Text."',
       '".$this->edFax->Text."',
       '".$this->edEmail->Text."')") or die("INSERT CONTACT - ".mysql_error());
       }

       if($this->hiddenComp->getValue()!=0)
       {
       mysql_query("INSERT INTO company_contact(company_id,
       fname,
       mname,
       lname,
       designation,
       department,
       bdate,
       acode,
       phone,
       mobile,
       address,
       bldg,
       region,
       province,
       municipality,
       fcode,
       fax,
       email)

       VALUES
       ('".$this->hiddenComp->getValue()."',
       '".$this->edContactFname->Text."',
       '".$this->edContactMname->Text."',
       '".$this->edContactLname->Text."',
       '".$this->cmbJob->getItemIndex()."',
       '".$this->cmbDept->getItemIndex()."',
       '".$this->JTBdate->Text."',
       '".$acode."',
       '".$this->edPhone->Text."',
       '".$this->edMobile->Text."',
       '".$this->edAddress->Text."',
       '".$this->edBldg->Text."',
       '".$this->cmbRegion->getItemIndex()."',
       '".$this->cmbProvince->getItemIndex()."',
       '".$this->cmbMunicipality->getItemIndex()."',
       '".$this->edfcode->Text."',
       '".$this->edFax->Text."',
       '".$this->edEmail->Text."')") or die("INSERT CONTACT - ".mysql_error());
       }

       echo '<script language=javascript>
       alert("Added contact successfully");
       window.opener.JTContact.location.reload();
       var answer = confirm("Add another record?");
       if(answer)
       {
       window.location.href="comp_contact_create.php?comp_id='.$this->hiddenComp->getValue().'";
       }
       else
       {
       window.close();
       }
       //window.close()
       //window.opener.document.getElementById("JTContactList").src="vendor_contact_list.php";
       </script>';
       }
       }


       function btnContactCancelClick($sender, $params)
       {
       echo '<script language=javascript>
       window.close()</script>';


       }

}

global $application;

global $Add_contact;

//Creates the form
$Add_contact=new Add_contact($application);

//Read from resource file
$Add_contact->loadResource(__FILE__);

//Shows the form
$Add_contact->show();

?>